package com.example.service;

import com.example.mapper.EmpMapper;
import com.example.pojo.Emp;
import com.example.pojo.pageBean;
import com.example.service.impl.EmpServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpService implements EmpServiceImpl {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public pageBean list(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) empList;
        //3.封装pageBean对象
        return new pageBean(p.getTotal(),p.getResult());
    }

    @Override
    public Emp getEmpById(Integer id) {
       return empMapper.getEmpById(id);
    }

    @Override
    public void add(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.add(emp);
    }

    @Override
    public void deleteById(int[] ids) {
        empMapper.deleteById(ids);
    }

    @Override
    public Emp getEmpByNameAndPassword(Emp emp) {
       return empMapper.getEmpByNameAndPassword(emp);
    }

    @Override
    public void updateById(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
    }
}
